//
//  MyOrderViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/22.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "MyOrderViewController.h"

#import "AllOrderTableView.h"//全部TableView
#import "ObligationTableView.h"//待付款TableView
#import "CommittedTableView.h"//待发货TableView
#import "TerminalReceivingTableView.h"//待收货TableView
#import "EvaluationTableView.h"//待评价TableView

#import "AllOrderModel.h"//全部订单Model

@interface MyOrderViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@end

@implementation MyOrderViewController
{
    UIView *_slider;//滑动条
    
    UIScrollView *_scrollView;
    
    AllOrderTableView *_all;
    ObligationTableView *_obligation;
    CommittedTableView *_committed;
    TerminalReceivingTableView *_terminalReceiving;
    EvaluationTableView *_evaluation;
    
    NSMutableArray *_allOrderArray;
    NSMutableArray *_obligationArray;
    NSMutableArray *_committedArray;
    NSMutableArray *_terminalReceivingArray;
    NSMutableArray *_evaluationArray;
    
    float width;
    float height;
    
    NSMutableArray *_labelArray;
    NSMutableArray *_buttonArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //设置显示页面
    [self setSlider];
    
    //网络请求
    [self network];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    width = WIDTH / 5;
    height = 40;
    
    //设置导航栏
    [self setNav];
    
    //创建滑动View
    [self createSliderView];
    
    //设置ScrollView
    [self setScrollView];
    
    //设置TableView
    [self setTableView];
}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    self.view.backgroundColor = BackgroundCOLOR;
    self.title = @"我的订单";
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

#pragma mark === 创建滑动View ===
- (void)createSliderView{
    
    NSArray *arr = @[@"全部", @"待付款", @"待发货", @"待收货", @"待评价"];
    _labelArray = [NSMutableArray array];
    _buttonArray = [NSMutableArray array];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, height)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    for (int i = 0; i < 5; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * width, 0, width, height);
        btn.tag = 11110 + i;
        [btn addTarget:self action:@selector(slider:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        UILabel *label = [[UILabel alloc] initWithFrame:btn.frame];
        label.text = arr[i];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = btn.tag + 10;
        [view addSubview:label];
        if (i == 0){
            label.textColor = APPCOLOR;
        }
        [_labelArray addObject:label];
        [_buttonArray addObject:btn];
    }
    _slider = [[UIView alloc] initWithFrame:CGRectMake(8, height - 3, width - 16, 3)];
    _slider.backgroundColor = APPCOLOR;
    [view addSubview:_slider];
    [view bringSubviewToFront:_slider];
}

- (void)slider:(UIButton *)sender{
    for (int i = 0; i < _labelArray.count; i++) {
        UILabel *label = _labelArray[i];
        UIButton *button = _buttonArray[i];
        if (button == sender) {
            label.textColor = APPCOLOR;
            [UIView animateWithDuration:0.2 animations:^{
                _slider.frame = CGRectMake( 8 + i * width, height - 3, width - 16, 3);
            }];
            _scrollView.contentOffset = CGPointMake(i * WIDTH, 0);
        }else label.textColor = [UIColor blackColor];
    }
}
#pragma mark === 设置ScrollView ===
- (void)setScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 107, WIDTH, HEIGHT)];
    //实际尺寸
    _scrollView.contentSize = CGSizeMake( WIDTH * 5, HEIGHT - 94);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        _slider.frame = CGRectMake(8 + (scrollView.contentOffset.x - 8) / 5, height - 3, width - 16, 3);
        
        for (UILabel *label in _labelArray) {
            label.textColor = [UIColor blackColor];
        }
        //全部
        if (scrollView.contentOffset.x < WIDTH / 2) {
            UILabel *label = (id)[self.view viewWithTag:11110 + 10];
            label.textColor = APPCOLOR;
        //待付款
        }else if (scrollView.contentOffset.x > WIDTH / 2&& scrollView.contentOffset.x < WIDTH * 3 / 2) {
            UILabel *label = (id)[self.view viewWithTag:11110 + 11];
            label.textColor = APPCOLOR;
        //待发货
        }else if (scrollView.contentOffset.x > WIDTH * 3 / 2 && scrollView.contentOffset.x < WIDTH * 5 / 2){
            UILabel *label = (id)[self.view viewWithTag:11110 + 12];
            label.textColor = APPCOLOR;
        //待收货
        }else if (scrollView.contentOffset.x > WIDTH * 5 / 2 && scrollView.contentOffset.x < WIDTH * 7 / 2){
            UILabel *label = (id)[self.view viewWithTag:11110 + 13];
            label.textColor = APPCOLOR;
        //待评价
        }else if (scrollView.contentOffset.x > WIDTH * 7 / 2 && scrollView.contentOffset.x < WIDTH * 5){
            UILabel *label = (id)[self.view viewWithTag:11110 + 14];
            label.textColor = APPCOLOR;
        }else if (scrollView.contentOffset.x < - WIDTH / 2){
            _slider.frame = CGRectMake(8, height - 3, width - 16, 3);
        }else if (scrollView.contentOffset.x > WIDTH * 5){
            _slider.frame = CGRectMake(WIDTH * 4 / 5 - 8, height - 3, width - 16, 3);
        }
    }
}
#pragma mark === 设置TableView ===
- (void)setTableView{
    _all = [[AllOrderTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 94)];
    [_scrollView addSubview:_all];
    _allOrderArray = [NSMutableArray array];
    
    _obligation = [[ObligationTableView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT - 94)];
    [_scrollView addSubview:_obligation];
    _obligationArray = [NSMutableArray array];
    
    _committed = [[CommittedTableView alloc] initWithFrame:CGRectMake(WIDTH * 2, 0, WIDTH, HEIGHT - 94)];
    [_scrollView addSubview:_committed];
    _committedArray = [NSMutableArray array];
    
    _terminalReceiving = [[TerminalReceivingTableView alloc] initWithFrame:CGRectMake(WIDTH * 3, 0, WIDTH, HEIGHT - 94)];
    [_scrollView addSubview:_terminalReceiving];
    _terminalReceivingArray = [NSMutableArray array];
    
    _evaluation = [[EvaluationTableView alloc] initWithFrame:CGRectMake(WIDTH * 4, 0, WIDTH, HEIGHT - 94)];
    [_scrollView addSubview:_evaluation];
    _evaluationArray = [NSMutableArray array];
}

#pragma mark === 设置显示页面 ===
- (void)setSlider{
    for (UILabel *label in _labelArray) {
        label.textColor = [UIColor blackColor];
    }
    UILabel *label = (id)[self.view viewWithTag:11110 + 10 + _index];
    label.textColor = APPCOLOR;
    _slider.frame = CGRectMake(8 + (WIDTH / 5) * _index, height - 3, WIDTH / 5 - 16, 3);
    _scrollView.contentOffset = CGPointMake(_index * WIDTH, 0);
}

#pragma mark === 导航栏Item ===
- (void)search{
    NSLog(@"搜索");
}
- (void)more{
    NSLog(@"更多");
}

#pragma mark === 网络请求 ===
- (void)network{
    
}
@end
